package cn.com.oceansoft.nos.taglib;


import cn.com.oceansoft.nos.common.constant.Constant;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import java.util.Set;


/**
 * Created by yixin on 2015/1/26.
 */
public class HasPrivilegeTag extends TagSupport {

    private String privilegeId;

    @Override
    public int doStartTag() throws JspException {
        HttpSession session = pageContext.getSession();
        if (privilegeJudge(session, privilegeId)){
            return EVAL_BODY_INCLUDE;
        }else{
            return SKIP_BODY;
        }
    }

    @Override
    public int doEndTag() throws JspException {
        return EVAL_PAGE;
    }

    private boolean privilegeJudge(HttpSession session,String privilegeId){
        if (!StringUtils.hasText(privilegeId)){
            return false;
        }
        Set<String> pris = (Set<String>) session.getAttribute(Constant.SESSION_USER_PRIVILEGE);
        boolean has = false;
        for (String pri : pris){
            if (pri.trim().equals(privilegeId)){
                has = true;
                break;
            }
        }
        return has;
    }

    public String getPrivilegeId() {
        return privilegeId;
    }

    public void setPrivilegeId(String privilegeId) {
        this.privilegeId = privilegeId;
    }
}
